package cn.datawin.http;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.date.DateUtil;
import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSONObject;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static cn.hutool.core.date.DatePattern.PURE_DATETIME_PATTERN;

public class JsonDemo {


    public static void main(String[] args) {
        long t1 = System.currentTimeMillis();
        String timestamp = DateUtil.format(new Date(), PURE_DATETIME_PATTERN);
        String accessAccount = "20001";
        String accessKey = "123456";
        String nonce = Base64.encode(accessAccount+","+timestamp);
        JSONObject params = new JSONObject();
        params.put("nonce",nonce);
        String signature = DigestUtil.md5Hex(Base64.decodeStr(accessAccount + DigestUtil.md5Hex(accessKey) + timestamp, Charset.defaultCharset()));
        params.put("signature",signature);
        List<String> phones = new ArrayList<>();
        phones.add("15806116068");

        params.put("phones",phones);
        HttpRequest request = HttpUtil.createPost("http://localhost:9000/check")
                .header("Content-Type","application/json;charset=UTF-8")
                .body(params.toJSONString());

        HttpResponse execute = request.execute();
        long t2 = System.currentTimeMillis();
        String body = execute.body();
        System.out.println(body+"------"+(t2-t1)+"ms");
    }
}
